package com.yibai.web.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yibai.web.app.entity.Category;
import com.yibai.web.app.entity.Website;
import com.yibai.web.app.mapper.CategoryMapper;
import com.yibai.web.app.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yibai.web.dto.CategoryInfoRequest;
import com.yibai.web.dto.CategoryListRequest;
import com.yibai.web.dto.CategoryRequest;
import com.yibai.web.dto.SuperResponse;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fang
 * @since 2021-04-19
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Override
    public SuperResponse list(CategoryListRequest request) {
        SuperResponse response = new SuperResponse();
        QueryWrapper<Category> wrapper = new QueryWrapper();
        Page<Category> pages = new Page<>(request.getPageNo(),request.getPageSize());
        IPage<Category> mapIPage = baseMapper.selectPage(pages, wrapper);
        List<Category> agents = mapIPage.getRecords();
        response.setItems(agents);
        response.setTotals(pages.getTotal());
        return response;
    }

    @Override
    public SuperResponse info(CategoryRequest request) {
        SuperResponse response = new SuperResponse();
        response.setItem(baseMapper.selectById(request.getId()));
        return response;
    }

    @Override
    public SuperResponse add(CategoryInfoRequest request) {
        SuperResponse response = new SuperResponse();
        Category category = new Category();
        category.setName(request.getName());
        category.setSiteId(request.getSiteId());
        category.setPinyin(request.getPinyin());
        category.setStatus(request.getStatus());
        category.setSortNo(request.getSortNo());
        baseMapper.insert(category);
        return response;
    }

    @Override
    public SuperResponse update(CategoryInfoRequest request) {
        SuperResponse response = new SuperResponse();
        Category category = new Category();
        category.setId(request.getId());
        category.setName(request.getName());
        category.setSiteId(request.getSiteId());
        category.setPinyin(request.getPinyin());
        category.setStatus(request.getStatus());
        category.setSortNo(request.getSortNo());
        baseMapper.updateById(category);
        return response;
    }

    @Override
    public SuperResponse delete(CategoryRequest request) {
        SuperResponse response = new SuperResponse();
        baseMapper.deleteById(request.getId());
        return response;
    }


    @Override
    public SuperResponse findCategoryBySid(CategoryRequest request) {
        SuperResponse response = new SuperResponse();
        QueryWrapper<Category> queryWrapper = new QueryWrapper();
        queryWrapper.eq("site_id",request.getSiteId());
        response.setItems(baseMapper.selectList(queryWrapper));
        return response;
    }
}
